<template>
  <div class="app-container">
    <div style="background-color: white;padding: 10px 10px 0px 10px">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="小组名称" prop="groupName">
          <el-input
            v-model="queryParams.groupName"
            placeholder="请输入小组名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="学校名称" prop="SchoolName">
          <el-input
            v-model="queryParams.SchoolName"
            placeholder="请输入学校名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="background-color: white;margin-top: 10px;padding: 20px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['patrol:group:add']"
          >新增
          </el-button>
        </el-col>
        <!--      <el-col :span="1.5">-->
        <!--        <el-button-->
        <!--          type="success"-->
        <!--          icon="el-icon-edit"-->
        <!--          size="mini"-->
        <!--          :disabled="single"-->
        <!--          @click="handleUpdate"-->
        <!--          v-hasPermi="['patrol:group:edit']"-->
        <!--        >修改</el-button>-->
        <!--      </el-col>-->
        <!--      <el-col :span="1.5">-->
        <!--        <el-button-->
        <!--          type="danger"-->
        <!--          icon="el-icon-delete"-->
        <!--          size="mini"-->
        <!--          :disabled="multiple"-->
        <!--          @click="handleDelete"-->
        <!--          v-hasPermi="['patrol:group:remove']"-->
        <!--        >删除</el-button>-->
        <!--      </el-col>-->
        <!--      <el-col :span="1.5">-->
        <!--        <el-button-->
        <!--          type="warning"-->
        <!--          icon="el-icon-download"-->
        <!--          size="mini"-->
        <!--          @click="handleExport"-->
        <!--          v-hasPermi="['patrol:group:export']"-->
        <!--        >导出</el-button>-->
        <!--      </el-col>-->
        <!--        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
      </el-row>

      <el-table v-loading="loading" :data="groupList" :header-cell-style="{background:'#E3E3E3'}">
        <!--        <el-table-column label="小组编号" align="center" prop="groupId"/>-->
        <el-table-column label="学校名称" align="center" prop="schoolName"/>
        <el-table-column label="小组名称" align="center" prop="groupName"/>
        <el-table-column label="小组类型" align="center" prop="groupType">
          <template slot-scope="scope">
            <span>{{ scope.row.groupType == false ? "行政" : "安保" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="小组成员" align="center">
          <template v-slot="scope">
            <span>{{ getbox(scope.row) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['patrol:group:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['patrol:group:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改学校小组对话框 -->
      <el-dialog :title="title" :visible.sync="open" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="小组名称" prop="groupName">
            <el-input v-model="form.groupName" placeholder="请输入小组名称"/>
          </el-form-item>
          <el-form-item label="学校名称" prop="groupSchoolId">
            <el-select v-model="form.groupSchoolId" disabled filterable placeholder="选择学校名称" size="large">
              <el-option
                v-for="item in schoolList"
                :key="item.coordIndex"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="小组类别" prop="groupType">
            <!--            <el-switch-->
            <!--              v-model="form.groupType"-->
            <!--              v-if="form.groupId==null"-->
            <!--              size="large"-->
            <!--              active-text="保安"-->
            <!--              inactive-text="行政"-->
            <!--              @change="(v)=>getschooluser(v,0,true)"-->
            <!--            />-->
            <!--            <span v-else>{{ form.groupType == false ? "行政" : "保安" }}</span>-->

            <el-radio-group v-model="form.groupType" @change="(v)=>getschooluser(v,0,true)">
              <el-radio :label="false" border>行政</el-radio>
              <el-radio :label="true" border>保安</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="小组成员" prop="userlist">
            <el-transfer
              :titles="['可选成员', '已选择']"
              v-model="userlist"
              filterable
              filter-placeholder="搜索成员"
              :data="groupuserlist"
              @change="(v)=>setformuserlist(v)"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {listGroup, getGroup, delGroup, addGroup, updateGroup, exportGroup, schoolList} from "@/api/patrol/group";
import {listGuard} from "@/api/patrol/guard";
import {listStaff} from "@/api/patrol/staff";
import log from "@/views/monitor/job/log";

export default {
  name: "Group",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      groupSchoolId: 0,
      staff: [],
      guard: [],
      // 总条数
      total: 0,
      // 学校小组表格数据
      groupList: [],
      schoolList: [],
      userlist: [],
      groupuserlist: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupName: null,
        groupSchoolId: null,
        SchoolName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        groupName: [
          {required: true, message: "小组名不可为空", trigger: "blur"},
          {message: "请输入中文", trigger: "blur", pattern: /^[\u0391-\uFFE5A-Za-z]+$/,},
        ],
        groupSchoolId: [
          {required: true, message: "学校名不可为空", trigger: "blur"}
        ],
        userlist: [
          {required: true, message: "成员不可为空", trigger: "blur"}
        ],
        groupType: [
          {required: true, message: "成员不可为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    listStaff().then(value => {
      this.staff = value.rows
      this.getList();
    })
    listGuard().then(value => {
      this.guard = value.rows
      this.getList();
    })
    schoolList().then(requires => {
      this.groupSchoolId = requires.rows[0]['id']
      this.schoolList = requires.rows
      console.log(this.schoolList)
    })
  },
  methods: {
    setformuserlist(v) {
      this.form.userlist = v
    },
    radioChange(e) {
      console.log(e)
    },
    getschooluser(x, groupid, add) {
      this.groupuserlist = []
      this.userlist = []
      console.log(add == true)
      if (!x) {
        listStaff().then(require => {
          require.rows.forEach((v) => {
            console.log(v)
            if (v.schoolGroupId == null || v.schoolGroupId == groupid) {
              this.groupuserlist.push(
                {
                  label: v.staffName,
                  key: v.id,
                }
              )
            }
            if (v.schoolGroupId == groupid)
              this.userlist.push(v.id)
          })
          if (!add)
            getGroup(groupid).then(response => {
              this.form = response.data;
              this.form.userlistx = this.userlist
              this.form.userlist = this.userlist
              this.open = true;
              this.title = "修改学校小组";
              console.log(this.form.userlist)
            });
        })
      } else
        listGuard().then(require => {
          require.rows.forEach((v) => {
            if (v.schoolGroupId == null || v.schoolGroupId == groupid)
              this.groupuserlist.push(
                {
                  label: v.guardName,
                  key: v.id,
                }
              )
            if (v.schoolGroupId == groupid)
              this.userlist.push(v.id)
          })
          if (!add)
            getGroup(groupid).then(response => {
              this.form = response.data;
              this.form.userlistx = this.userlist
              this.form.userlist = this.userlist
              this.open = true;
              this.title = "修改学校小组";
              console.log(this.form.userlist)
            });
        })

    },
    getbox(group) {
      var s = ''
      console.log(this.guard.length)
      if (group.groupType)
        this.guard.forEach((v) => {
          if (v.schoolGroupId === group.groupId) {
            s += v.guardName + ","
            s += "\n"
          }
        })
      else this.staff.forEach((v) => {
        if (v.schoolGroupId === group.groupId) {
          s += v.staffName + ","
          s += "\n"
        }
      })
      return s.substring(0, s.length - 2)
    },
    /** 查询学校小组列表 */
    getList() {
      this.loading = true;
      listGroup(this.queryParams).then(response => {
        console.log(response.rows)
        this.groupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        groupId: null,
        groupName: null,
        groupSchoolId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        groupType: false,
        userlist: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.groupId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.groupSchoolId = this.groupSchoolId
      this.userlist = []
      this.open = true;
      this.getschooluser(false, 0, true)
      this.title = "添加巡查小组";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.userlist = []
      const groupId = row.groupId || this.ids
      this.getschooluser(row.groupType, groupId)
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form)
      if (this.form.userlist.length <= 0) {
        this.$message.error("成员不能为空")
        return
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.groupId != null) {
            updateGroup(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.$tab.refreshPage();
            });
          } else {
            addGroup(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.$tab.refreshPage();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const groupIds = row.groupId || this.ids;
      this.$confirm('是否确认删除选择的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delGroup(groupIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有学校小组数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportGroup(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
<style scoped>
.app-container {
  background-color: #edf1f2;
  padding: 10px;
  min-height: calc(100vh - 35px)
}

::v-deep.pagination-container .el-pagination {
  text-align: center;
  position: sticky;
}
</style>
